package fy.mdm.dao;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONObject;

import com.soa.eis.adapter.framework.message.IMsgObject;
import com.soa.eis.adapter.framework.message.impl.GroupRecord;
import com.soa.eis.adapter.framework.message.impl.MsgObject;
import com.soa.eis.adapter.framework.requester.IServiceRequester;
import com.soa.eis.adapter.framework.requester.impl.BaseServiceRequester;

import fy.mdm.bean.FiexdAssetsBean;

public class FiexdAssetsDao {

	private static Log log = LogFactory.getLog(FiexdAssetsDao.class);
	private String[] fiexdCode;// ={"M000125"};
	private String[] lineCode;//={"S1HP01"};

	public JSONObject getFiexdAssetsInfo() throws Exception {
		JSONObject jo = new JSONObject();
		for (int i = 0, length = this.fiexdCode.length; i < length; i++) {
			FiexdAssetsBean fiexdAssetsBean = new FiexdAssetsBean();
			IServiceRequester Requester = BaseServiceRequester.getInstance();
			// List<GroupRecord> grs1 = new ArrayList<GroupRecord>();
			// 定义请求报文对象
			IMsgObject reqMo = new MsgObject(IMsgObject.MOType.initSR);
			String SourceSysID = "06004"; // 请求方(发布方)
			String ServiceID = "06001000000030";// 响应方(发布方)
			// 获取系统时间
			Date date = new Date();
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
					"yyyyMMddHHmmss");
			String format = simpleDateFormat.format(date);
			// 对请求报文对象进行设置
			reqMo.setSourceSysID(SourceSysID); // 设置报文 请求方系统号
			reqMo.setSerialNO(format.substring(0, 8)
					+ SourceSysID
					+ String.format("%06d",
							++MaterialRequestNoTimerTask.requestNo));// 设置报文流水号,第三段表示今天发送的第几的一次报文
			reqMo.setServiceID(ServiceID); // 设置报文 提供方服务号
			reqMo.setServiceDateTime(format); // 设置报文 日期时间
			// reqMo.setReqValue("materrialCode",materialCode[i]);
			log.info("fiexdCode:No" + i + "  " + fiexdCode[i]);
//			log.info("lineCode:No" + i + "  " + lineCode[i]);

			if (lineCode != null ) {
				for (int j = 0; j < lineCode.length; j++) {
					reqMo.setReqValue("PRO_LINE_CODE", lineCode[j]);
					log.info("reqMo:" + reqMo);
					IMsgObject respMo = null;

					respMo = Requester.execute(reqMo);
					// System.out.println(respMo+"999s");
					log.info("respMo:" + respMo);

					fiexdAssetsBean.setNewLineCode(lineCode[j]);

					List<GroupRecord> record2 = respMo.getResGroupRecord("ROW");
					for (GroupRecord groupRecord : record2) {
						fiexdAssetsBean.setNewLineName(groupRecord
								.getFieldValue("PRO_LINE_NAME"));

						// fiexdAssetsBean.setAssetNmuber(groupRecord.getFieldValue("ASSETNUMBER"));

					}
					// jo.put(fiexdAssetsBean.getLINE(),fiexdAssetsBean.getJSONObject());
				}
			}
			if (fiexdCode != null) {
				reqMo.setReqValue("EQUIPMENT_CODE", fiexdCode[i]);
				log.info("reqMo:" + reqMo);
				IMsgObject respMo = null;

				respMo = Requester.execute(reqMo);
				// System.out.println(respMo+"999s");
				log.info("respMo:" + respMo);

				fiexdAssetsBean.setAssetBarcode(fiexdCode[i]);

				List<GroupRecord> record2 = respMo.getResGroupRecord("ROW");
				for (GroupRecord groupRecord : record2) {
					fiexdAssetsBean.setAssetSpeModel(groupRecord
							.getFieldValue("EQUIPMENT_MODEL")); // 规格型号
					fiexdAssetsBean.setAssetName(groupRecord
							.getFieldValue("MANUFACTURER")); // 供应商名称
					fiexdAssetsBean.setAssetNmuber(groupRecord
							.getFieldValue("EQUIPMENT_TYPE_CODE")); // 设备类型
					fiexdAssetsBean.setLineName(groupRecord
							.getFieldValue("PRO_LINE_NAME"));// 生产线名称
					fiexdAssetsBean.setLineCode(groupRecord
							.getFieldValue("PRO_LINE_CODE"));// 生产线代码
					fiexdAssetsBean.setAssetsName(groupRecord
							.getFieldValue("EQUIPMENT_CATE_NAME")); // 设备名称

					// fiexdAssetsBean.setNewLineName(groupRecord
					// .getFieldValue("PRO_LINE_NAME"));

					// fiexdAssetsBean.setAssetNmuber(groupRecord.getFieldValue("ASSETNUMBER"));

				}

				jo.put(fiexdAssetsBean.getID(), fiexdAssetsBean.getJSONObject());
			}


		}


		log.info("jo:" + jo);
		return jo;
	}

	public void setCodes(String[] fiexdAssetsCodeArray) {
		this.fiexdCode = fiexdAssetsCodeArray;
	}

	public void setLines(String[] fiexdLineCodeArray) {
		this.lineCode = fiexdLineCodeArray;
	}


}
